class AddRightsToDeleteUsers < ActiveRecord::Migration
def self.create_right(name, controller, action)
    Right.create :name => name, :controller => controller, :action => action
  end
  
  def self.remove_right(controller, action)
    Right.find(:all, :conditions => "controller = '#{controller}' AND action = '#{action}'").each do |right|
      RoleRight.find(:all, :conditions => "right_id = #{right.id}").each do |role_right|
        role_right.destroy
      end
      right.destroy
    end
  end
  
  def self.up
    rights = {
      :users => {
        :delete_selected => create_right('Poista valitut', 'users', 'destroy_selected')
      }
    }
    roles = {:power_user => Role.find_by_name('Tehokäyttäjä'),
      :admin => Role.find_by_name('Järjestelmänvalvoja')}
    roles[:admin].rights << rights[:users][:delete_selected]
  end
  
  def self.down
    remove_right('users', 'delete_selected')
  end
end
